package com.secsbrain.cmdb.web.controller;

import java.util.Date;
import java.util.List;

import com.secsbrain.cmdb.biz.IAppTypeService;
import com.secsbrain.sso.core.SsoHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.cmdb.dal.model.AppType;

/**
 * 应用分类控制层
 * @author zhaoyi
 *
 */
@RestController
@RequestMapping("api/appType")
public class AppTypeController{
	
    @Autowired
    IAppTypeService appTypeService;
    
    /**
     * 功能描述: <br>
     * 查询列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseResult<?> queryList(Integer page, Integer limit,AppType params) {
    	if(StringUtils.isEmpty(params.getDepartment())){
    		params.setDepartment(null);
    	}
    	Page<AppType> pageBean=new Page<AppType>(page, limit);
    	Page<AppType> result=appTypeService.selectPage(pageBean, params);
    	return new ResponseResult(result);
    }
    
    /**
     * 功能描述: <br>
     * 查询所有列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/allList", method = RequestMethod.GET)
    public ResponseResult<?> queryAllList(AppType params) {
    	List<AppType> list=appTypeService.selectList(params);
    	for(AppType appType:list){
    	    if(StringUtils.isEmpty(appType.getDepartment())){
                appType.setName(appType.getName());
            }else{
                appType.setName(appType.getDepartment()+"-"+appType.getName());
            }
    	}
    	return new ResponseResult(list);
    }
    
    /**
     * 功能描述: <br>
     * 新增
     *
     * @param appType
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult<?> insert(@RequestBody AppType appType) {
    	appType.setCreateTime(new Date());
    	appType.setCreator(SsoHelper.currentUser().getNickname());
    	appTypeService.insertSelective(appType);
    	return new ResponseResult();
    }
    
    /**
     * 功能描述: <br>
     * 更新
     *
     * @param appType
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseResult<?> update(@RequestBody AppType appType) {
    	appType.setUpdateTime(new Date());
    	appType.setUpdator(SsoHelper.currentUser().getNickname());
    	appTypeService.updateSelectiveById(appType);
    	return new ResponseResult();
    }
    
    /**
     * 功能描述: <br>
     * 删除
     *
     * @param appType
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseResult<?> delete(@RequestBody AppType appType) {
    	appTypeService.deleteById(appType.getId());
    	return new ResponseResult();
    }
    
}
